home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Networking / TCP Server / queues.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  2.0 KB  |  100 lines  |  [TEXT/KAHL]

  1. /*
  2.     TCP Client/Server Queuing Example
  3.     Steve Falkenburg, MacDTS, Apple Computer
  4.     3/11/92
  5.     
  6.     this client/server sample uses MacTCP to implement a simple "greeting" server.  the server
  7.     opens up several listeners on kGreetingPort (1235).  when a client connects, the data entered
  8.     in the greeting dialog is sent to the remote connection, and the connection is closed.
  9.     
  10.     connection management is done through the use of Operating System queues to simplify tracking
  11.     and usage.
  12. */
  13.  
  14.  
  15. #include "const.h"
  16. #include "globals.h"
  17. #include "utils.h"
  18. #include "queues.h"
  19.  
  20. static    QHdr        gFreeQueue,gCompletedQueue;        // our queue header structures
  21. static    MyQElem        gBufferPool[kNumBuffers];        // queue storage area is here
  22.  
  23.  
  24. /* initializes our queue handling scheme */
  25.  
  26. void InitQueues(void)
  27. {
  28.     short i;
  29.     
  30.     gFreeQueue.qFlags = 0;
  31.     gFreeQueue.qHead = 0;
  32.     gFreeQueue.qTail = 0;
  33.     
  34.     gCompletedQueue.qFlags = 0;
  35.     gCompletedQueue.qHead = 0;
  36.     gCompletedQueue.qTail = 0;
  37.     
  38.     gFree = kNumBuffers;
  39.     gRunning = gCompleted = 0;
  40.     gServiced = 0;
  41.     
  42.     for (i=0; i<kNumBuffers; i++)
  43.         Enqueue(&gBufferPool[i],&gFreeQueue);
  44. }
  45.  
  46.  
  47. /* pulls a parameter block off of the unused queue to be used in a device manager call
  48. */
  49.  
  50. MyQElemPtr GetUnusedPBlock(void)
  51. {
  52.     MyQElemPtr retValue;
  53.     
  54.     retValue = (MyQElemPtr) gFreeQueue.qHead;
  55.     if (retValue) {
  56.         Dequeue(retValue,&gFreeQueue);
  57.         gFree--;
  58.     }
  59.     
  60.     return retValue;
  61. }
  62.  
  63.  
  64. /*    puts a parameter block back onto the unused queue to be picked up and used by someone else
  65. */
  66.  
  67. void RecycleFreePBlock(MyQElemPtr pBlock)
  68. {
  69.     Enqueue(pBlock,&gFreeQueue);
  70.     gFree++;
  71. }
  72.  
  73.  
  74. /*    pulls a parameter block off of the completed queue for processing by the caller
  75. */
  76.  
  77. MyQElemPtr GetCompletedPBlock(void)
  78. {
  79.     MyQElemPtr retValue;
  80.     
  81.     retValue = (MyQElemPtr) gCompletedQueue.qHead;
  82.     if (retValue) {
  83.         Dequeue(retValue,&gCompletedQueue);
  84.         gCompleted--;
  85.     }
  86.     
  87.     return retValue;
  88. }
  89.  
  90.  
  91. /*    queues a parameter block onto the completed queue.  this is normally called from completion
  92.     routines at interrupt time.
  93. */
  94.  
  95. void StoreCompletedPBlock(MyQElemPtr pBlock)
  96. {
  97.     Enqueue(pBlock,&gCompletedQueue);
  98.     gCompleted++;
  99. }
  100.